package org.ccay.validator.validators;

import com.opensymphony.xwork2.validator.ValidationException;

/**
 * 表达式验证器的抽象类
 * 
 *
 */
public abstract class AbstractExpressionValidator extends AbstractFieldValidator{

	protected String expression;


    public void setExpression(String expression) {
        this.expression = expression;
    }

    public String getExpression() {
        return expression;
    }

    public void validate(Object object) throws ValidationException {
        String fieldName = getFieldName();

        Boolean answer = Boolean.FALSE;
        Object obj = null;

        try {
            obj = getFieldValue(expression, object);
        } catch (Exception e) {
            // let this pass, but it will be logged right below
        	log.error(e.getMessage(), e);
        }
        //验证是否通过
        boolean isBoolean = (obj != null) && (obj instanceof Boolean);
        if (isBoolean) {
            answer = (Boolean) obj;
        } else {
            log.warn("Got result of " + obj + " when trying to get Boolean.");
        }
       //验证不通过 插入异常信息
        if (!answer.booleanValue()) {
        	processMessage(fieldName, object);
        }
    }
    
    /**
     * 处理错误信息
     * 
     * @param fieldName
     * @param object
     */
    protected abstract void processMessage(String fieldName, Object object);

}
